/**
 * Created by luolh on 17/10/31.
 */

angular.module('partials.commission.controllers', [])
    .controller('commissionOrderDetailCtrl', ['$scope','$http', '$location', '$routeParams', function ($scope, $http, $location,$routeParams) {
        $scope.currentPage = 1;
        $scope.pageSize = 15;
        var orderTime =  $routeParams.orderTime;//$location.search().orderTime;
        var userId =  $routeParams.userId;//$location.search().userId;
        var startTime = orderTime + " 00:00:00";
        var endTime = orderTime + " 23:59:59";
        //分页
        $scope.pagination = new pagination({currentPage:$scope.currentPage,pageSize: $scope.pageSize,
            url:'http://' + backOfficeHost + '/rest/v1.0/commission/commissionOrderlList/:page/:size',
            ajax: $http,
            params: {startTime:startTime, endTime:endTime, userId: userId},
            success:function(data){
                $scope.user= data.data.user;
                $scope.orderList = data.data.list;
                $scope.pageNumber = data.data.totalPage;
                $scope.count = data.data.count;
                $scope.currentPage = $(".am-active").text();
                $scope.pagination.totalPage = data.data.totalPage;
                $scope.pagination.pageHeader($scope.pageNumber);
                $('#count').text("共"+$scope.pageNumber+"页 有"+$scope.count+"数据");
            },
            error: function(err){
                $scope.orderList = null;
            }
        });
        // 计算佣金
        $scope.doCommission = function(order,user){
            var total = 0;
            if ((order.shareCommissionUser && order.shareCommissionUser.objId == user)
                ||(order.selfPurchaseUse && order.selfPurchaseUser.objId == user)
                || (order.communityCommissionUser && order.communityCommissionUser.objId == user)) {
                if (order.shareCommission) {
                    total+=order.shareCommission;
                } else if (order.selfPurchase) {
                    total+=order.selfPurchase;
                } else if (order.communityCommission) {
                    total+=order.communityCommission;
                }
            } else {
                if (order.inviterCommission) {
                    total+=order.inviterCommission;
                }
            }
            return total;
        }
        // 计算结算状态
        $scope.doBalanceStatus = function(order,user){
            var balanceStatus = order.balanceStatus;
            if ((order.shareCommissionUser && order.shareCommissionUser.objId == user)
                ||(order.selfPurchaseUse && order.selfPurchaseUser.objId == user)
                || (order.communityCommissionUser && order.communityCommissionUser.objId == user)) {
                // 本单结算类型
                if (balanceStatus == 0) {
                    return "未处理";
                } else if (balanceStatus == 1) {
                    return "已粗算";
                } else if (balanceStatus == 2) {
                    return "已精算";
                } else if (balanceStatus == 3) {
                    return "已结算";
                } else if (balanceStatus == 4) {
                    return "已精算";
                } else if (balanceStatus == 5) {
                    return "已结算";
                }
            } else {
                // 提出结算类型
                if (balanceStatus == 0) {
                    return "未处理";
                } else if (balanceStatus == 1) {
                    return "已粗算";
                } else if (balanceStatus == 2) {
                    return "已精算";
                } else if (balanceStatus == 3) {
                    return "已精算";
                } else if (balanceStatus == 4) {
                    return "已结算";
                } else if (balanceStatus == 5) {
                    return "已结算";
                }
            }
        };
        $scope.commissionDrawPercentage = function(order,user){
            var commissionDrawPercentage = order.commissionDrawPercentage;
            if ((order.shareCommissionUser && order.shareCommissionUser.objId == user)
                ||(order.selfPurchaseUser && order.selfPurchaseUser.objId == user)
                || (order.communityCommissionUser && order.communityCommissionUser.objId == user)) {
                // 本单结算类型
                return "";
            } else {
                // 提出结算类型
                return (commissionDrawPercentage * 100).toFixed(2)+"%";
            }
        };
        //搜索
        // $scope.search = function (temp) {
        //     var current = temp.target;
        //     if (current) {
        //         $scope.currentPage = $scope.pagination.currentPage;
        //     }else {
        //         $scope.pagination.currentPage = 1;
        //         $scope.currentPage = 1;
        //     }
        //     var body = {};
        //     if (orderTime) body.orderTime = orderTime;
        //     if (userId) body.userId = userId;
        //
        //     $http.post('http://' + backOfficeHost + '/rest/v1.0/commission/commissionOrderlList/' + $scope.currentPage + '/' + $scope.pageSize, body, {
        //         headers: {'Content-Type': 'application/json;charset=utf-8'}
        //     }).success(function (data) {
        //         $scope.orderList = data.data.list;
        //         $scope.pageNumber = data.data.totalPage;
        //         $scope.count = data.data.count;
        //
        //         $scope.pagination.totalPage = data.data.totalPage;
        //         $scope.pagination.pageHeader($scope.pageNumber);
        //         $scope.pagination.searchParams = body;
        //     }).error(function (err) {
        //         $scope.orderList = null;
        //         alert(err);
        //     });
        // };

        // 导出
        $scope.exportAmbassadorOrderDatas = function () {
            var startTime = orderTime + " 00:00:00";
            var endTime = orderTime + " 23:59:59";

            var href = 'http://' + backOfficeHost + '/rest/v1.0/commission/exportAmbassadorOrderDatas?'
                    +'userId='+userId
                    +'&startTime='+startTime
                    +"&endTime="+endTime;
            location.href =href;
        }
    }]);